package com.atguigu.spzx.manager.service.impl;

import cn.hutool.captcha.CircleCaptcha;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Author yaya
 * @Create 2024/6/19 18:49
 * Description:
 */
@Service
public class ValidateCodeServiceImpl implements ValidateCodeService {
    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Override
    public ValidateCodeVo generateValidateCode() {
        ValidateCodeVo validateCodeVo = new ValidateCodeVo();
        // 创建验证码
        CircleCaptcha captcha = new CircleCaptcha(150,48,4,50);
        validateCodeVo.setCodeKey(UUID.randomUUID().toString().replace("-",""));
        validateCodeVo.setCodeValue(captcha.getImageBase64Data());

        // key:UUID, value: code
        redisTemplate.opsForValue().set(validateCodeVo.getCodeKey(), captcha.getCode());

        return validateCodeVo;
    }
}
